This notebook will demonstrate the process of using a deep neural network in R to perform image classification.
Necessary libraries:
library(gdata)
library("jpeg")
library("imager")
We use the following function to load the images:
getFileNameAndExtension <- function(file){
ex <- strsplit(basename(file), split="\\.")
ex<-unlist(ex)
return(ex)
}
firstVideoFileName<-'marker.mov'
secondVideoFileName<-'bottle.mov'
outFilePrefix='out'
outFileSuffix='.jpg'
thumbNailFolder<-"Small"
nameAndExtension<-getFileNameAndExtension(firstVideoFileName)
fnames <- paste0(thumbNailFolder,nameAndExtension[[1]],"/","out", 2, ".jpg")
fnames
sampleMarker<-readJPEG(fnames,native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(sampleMarker,0,0,1,1)
nameAndExtension<-getFileNameAndExtension(secondVideoFileName)
fnames <- paste0(thumbNailFolder,nameAndExtension[[1]],"/","out", 2, ".jpg")
fnames
sampleBottle<-readJPEG(fnames,native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(sampleBottle,0,0,1,1)
We will use the grayscale intensity of each image as the feature input for a neural network.
dim(sampleMarker)
gs.matrix<-as.matrix(sampleMarker)
dim(gs.matrix)
i.vector<-unmatrix(gs.matrix,byrow=T)
length(i.vector) # unmatrix the gray intensity image as a single column vector, which will be used as feature values to train neural network
Next we label the images with the following function:
loadImagesfromFolderAndLabel <- function(pathName,prefix, clasLabel) {
imageNames<-paste0(prefix,"*.*")
files<-list.files(path=pathName,pattern=imageNames,all.files=T,full.name=T,no..=T)
list_of_images=lapply(files,load.image)
#list_of_images=lapply(files,read.pnm)
#plot(list_of_images[[1]])
n.images<-length(list_of_images)
thmb.matrix<-as.matrix(list_of_images[[1]])
image.vector<-unmatrix(thmb.matrix,byrow=T)
for(ii in 2:n.images)
{
thmb.matrix<-as.matrix(list_of_images[[ii]])
i.vector<-unmatrix(thmb.matrix,byrow=T)
image.vector<-rbind(image.vector,i.vector)
}
image.frame<-data.frame(image.vector)
n.rows<-nrow(image.frame)
class1.label<-rep(clasLabel,n.rows)
image.frame<-cbind(image.frame,class1.label)
rm(list_of_images)
return (image.frame)
}
We then create the input vectors:
nameAndExtension<-getFileNameAndExtension(firstVideoFileName)
folderName<-paste0(thumbNailFolder,nameAndExtension[[1]])
folderName
class1.frame <- loadImagesfromFolderAndLabel(folderName,outFilePrefix,1)
dim(class1.frame)
nameAndExtension<-getFileNameAndExtension(secondVideoFileName)
folderName<-paste0(thumbNailFolder,nameAndExtension[[1]])
folderName
class2.frame <- loadImagesfromFolderAndLabel(folderName,outFilePrefix,-1)
dim(class2.frame)
total.frame<-rbind(class1.frame,class2.frame)
dim(total.frame)
Then we create a training and test set, using 60% of the images for the training set and 40% for the test set:
train.index<-sample(nrow(total.frame),nrow(total.frame)*0.6)
training.set<-total.frame[train.index,]
training.set
test.set<-total.frame[-train.index,]
test.set
Next we train the neural network, using one hidden layer, 20 hidden nodes, and 500 repetitions:
library(neuralnet)
myform <- as.formula(paste('class1.label ~ ',paste(names(training.set[!names(training.set) %in% 'class1.label']), collapse = ' + ')))
oneHiddenLayerClassifier <- neuralnet(myform, training.set, hidden = 20, rep=500, linear.output = FALSE, threshold = 0.01)
summary(oneHiddenLayerClassifier)
Next we make classification predictions:
class.index<-length(test.set)
predictionResults<-compute(oneHiddenLayerClassifier,test.set[,-class.index])
predictionResults$net.result
classifications<-ifelse(predictionResults$net.result>0.5,1,-1)
classifications
table(test.set[,class.index],classifications)
Classification accuracy: 100%
For a performance comparison on the test set, we will train a second deep neural network with two hidden layers with a 5x4 set of hidden nodes in each layer:
myform <- as.formula(paste('class1.label ~ ',paste(names(training.set[!names(training.set) %in% 'class1.label']), collapse = ' + ')))
twoHiddenLayerClassifier <- neuralnet(myform, training.set, hidden = c(5, 4), rep=500, linear.output = FALSE, threshold = 0.01)
summary(twoHiddenLayerClassifier)
class.index<-length(test.set)
predictionResults<-compute(twoHiddenLayerClassifier,test.set[,-class.index])
predictionResults$net.result
classifications<-ifelse(predictionResults$net.result>0.5,1,-1)
classifications
table(test.set[,class.index],classifications)
Classification accuracy: 100%
So, there is no benefit to increasing complexity of the deep neural network for a toy classification problem this small.